package com.lds.erp.order.cuopon;

import com.dph.common.utils.base.BaseController;
import com.dph.common.utils.base.PageEntity;
import com.dph.common.utils.utils.AppUtils;
import com.dph.common.utils.utils.CommonUtils;
import com.lds.erp.base.JsonResult;
import com.lds.erp.base.baseset.order.GiftCardGrabOrder;
import com.lds.erp.base.baseset.order.GiftCardQueryOrder;
import com.lds.erp.base.baseset.result.GiftCardGrabResult;
import com.lds.erp.base.baseset.result.GiftCardQueryResult;
import com.lds.erp.common.util.AccountCache;
import com.lds.erp.common.util.AccountUtils;
import com.lds.erp.order.OrderCouponClient;
import com.lds.erp.order.cart.result.*;
import com.lds.erp.order.cart.vo.*;
import com.lds.erp.order.coupon.info.OrderCouponMchDetailInfo;
import com.lds.erp.order.coupon.order.MyCouponOrder;
import com.lds.erp.order.coupon.result.MyCouponResult;
import com.lds.erp.order.cuopon.ao.GiftCardQueryAO;
import com.lds.erp.order.cuopon.ao.MyCouponAO;
import com.lds.erp.order.cuopon.vo.GiftCardGrabVO;
import com.lds.erp.order.cuopon.vo.GiftCardQueryInfoVO;
import com.lds.erp.order.cuopon.vo.MyCouponVO;
import com.lds.erp.util.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liuli
 */
@RestController
@Api(value = "我的优惠券", description = "我的优惠券", tags = {"我的优惠券接口"})
@RequestMapping(value = "/api/order/coupon/")
public class CouponController extends BaseController {
    @Autowired
    private OrderCouponClient orderCouponClient;

    @ApiOperation(value = "我的优惠券", notes = "我的优惠券")
    @GetMapping("my_coupon")
    public JsonResult<MyCouponVO> updateCart(MyCouponAO ao) {

        MyCouponOrder order = new MyCouponOrder();
        order.setSerialNo(AppUtils.newNo());
        AccountCache accountCache = AccountUtils.getAccount();
        order.setMchId(accountCache.getMchId());
        order.setUsrType(ao.getUsrType());
        order.setPage(new PageEntity(ao.getPageNum(), ao.getPageSize()));
        MyCouponResult result = orderCouponClient.myCoupon(order);
        return result.isSuccess() ? JsonUtils.success(result, MyCouponVO.class) : JsonUtils.fail(result.getMessage());
    }


    @ApiOperation(value = "我的优惠券", notes = "我的优惠券")
    @GetMapping("my_coupon_show")
    public JsonResult<MyCouponVO> couponShow(MyCouponAO ao) {

        MyCouponOrder order = new MyCouponOrder();
        order.setSerialNo(AppUtils.newNo());
        AccountCache accountCache = AccountUtils.getAccount();
        order.setMchId(accountCache.getMchId());
        order.setUsrType(ao.getUsrType());
        order.setPage(new PageEntity(1, 100));
        MyCouponResult result = orderCouponClient.myCouponShow(order);
        return result.isSuccess() ? JsonUtils.success(result, MyCouponVO.class) : JsonUtils.fail(result.getMessage());
    }



    @ApiOperation(value = "红包页面显示", notes = "红包页面显示")
    @GetMapping("gift_query")
    public JsonResult<GiftCardQueryInfoVO> giftQuery() {
        GiftCardQueryOrder order = new GiftCardQueryOrder();
        order.setSerialNo(AppUtils.newNo());
        AccountCache accountCache = AccountUtils.getAccount();
        order.setMchId(accountCache.getMchId());
        GiftCardQueryResult result = orderCouponClient.giftCardQuery(order);
        return result.isSuccess() ? JsonUtils.success(result, GiftCardQueryInfoVO.class) : JsonUtils.fail(result.getMessage());
    }

    @ApiOperation(value = "抢红包", notes = "抢红包")
    @PostMapping("gift_grab")
    public JsonResult<GiftCardGrabVO> giftCardGrab(@RequestBody GiftCardQueryAO ao) {
        GiftCardGrabOrder order = new GiftCardGrabOrder();
        order.setSerialNo(AppUtils.newNo());
        AccountCache accountCache = AccountUtils.getAccount();
        order.setMchId(accountCache.getMchId());
        GiftCardGrabResult result = orderCouponClient.giftCardGrab(order);
        return result.isSuccess() ? JsonUtils.success(result, GiftCardGrabVO.class) : JsonUtils.fail(result.getMessage());
    }


}
